//
//  MakeSurePwdViewController.m
//  MM
//
//  Created by 郑渊文 on 5/21/15.
//  Copyright (c) 2015 王晨辉. All rights reserved.
//

#import "MakeSurePwdViewController.h"
#import "CommonMacro.h"
#import "myTabBarViewController.h"

@interface MakeSurePwdViewController ()<UITextFieldDelegate>
{
    UIView *myBgView;
}
@property(nonatomic,copy)UITextField *passWord;
@property(nonatomic,copy)UITextField *certainPassWord;
@property(nonatomic,copy)NSMutableDictionary *mydic;

@end

@implementation MakeSurePwdViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self layout];
    [self ifmutable];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)ifmutable
{
   if ([self.mydic isKindOfClass:[NSMutableDictionary class]]) {
    
    ////NSLog(@"可变!");
}else{
    ////NSLog(@"字典的值为%@",self.mydic);

    ////NSLog(@"完了之后字典的值为%@",self.mydic);
    ////NSLog(@"不可变!");
 }
}


-(void)layout
{
    
    if (IS_IPHONE_4_OR_LESS) {
        UIScrollView *bgView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT)];
        bgView.contentSize = CGSizeMake(SCREEN_WIDTH, 568);
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        myBgView = bgView;
    }
    else
    {
        
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        myBgView = bgView;
    }
    
    UIView *registView = [[UIView alloc]initWithFrame:CGRectMake(20,150, SCREEN_WIDTH-40, 160)];
    [myBgView addSubview:registView];
    registView.layer.cornerRadius = 8;
    registView.layer.borderColor = UIColorWithRGBA(232, 234, 237, 1).CGColor;
    registView.layer.borderWidth = 1;
    
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    self.title = @"找回密码";//设置navigationbar上显示的标题
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色
    
//    UILabel *sender = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 10, 200, 20)];
//    sender.text = @"你可以使用手机号码注册或者找回密码";
//    sender.textColor = [UIColor grayColor];
//    sender.textAlignment = NSTextAlignmentCenter;
//    sender.font = [UIFont systemFontOfSize:10];
//    [registView addSubview:sender];
    
//    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH-20, 1)];
    UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH-40, 1)];
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(0, 110, SCREEN_WIDTH-40, 1)];
    
//    line1 .backgroundColor = UIColorWithRGBA(232, 234, 237, 1);
    line2 .backgroundColor = UIColorWithRGBA(232, 234, 237, 1);
    line3 .backgroundColor = UIColorWithRGBA(232, 234, 237, 1);
    
    UIImageView *phoneImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 25, 12, 20)];
    phoneImg.image = [UIImage imageNamed:@"_0002s_0005_手机@2x"];
    [registView addSubview:phoneImg];
    
    UIImageView *passWordImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 75, 17, 20)];
    passWordImg.image = [UIImage imageNamed:@"_0002s_0008_密码@2x"];
    [registView addSubview:passWordImg];
    
    UIImageView *certainPassWordImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 125, 17, 20)];
    certainPassWordImg.image = [UIImage imageNamed:@"_0002s_0008_密码@2x"];
    [registView addSubview:certainPassWordImg];
    
    
    UILabel *phoneLab = [[UILabel alloc]initWithFrame:CGRectMake(60,20, SCREEN_WIDTH-40, 35)];
    phoneLab.text = _phoneNum;
    [registView addSubview:phoneLab];
    
    _passWord = [[UITextField alloc]initWithFrame:CGRectMake(60, 70, SCREEN_WIDTH-40, 35)];
    _passWord.delegate = self;
    _passWord.placeholder = @"请输入密码";
    _passWord.secureTextEntry = YES;
    
    _certainPassWord = [[UITextField alloc]initWithFrame:CGRectMake(60, 120, 200, 35)];
    _certainPassWord.delegate = self;
    _certainPassWord.placeholder = @"请输入确认密码";
    _certainPassWord.secureTextEntry = YES;
  
    [registView addSubview:_passWord];
    [registView addSubview:_certainPassWord];
    
//    [registView addSubview:line1];
    [registView addSubview:line2];
    [registView addSubview:line3];
    
    UIButton *nextTepBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 330, SCREEN_WIDTH-40, 50)];
    //    UIImage* image = [UIImage resizedImage:@"bg_green_button.9"];
    nextTepBtn.layer.cornerRadius = 8;
    nextTepBtn.backgroundColor = [GetFileModel colorWithHexString:@"#34bd81" alpha:1];
    [nextTepBtn setTitle:@"立即登录" forState:UIControlStateNormal];
    [nextTepBtn addTarget:self action:@selector(nextTep) forControlEvents:UIControlEventTouchUpInside];
    [myBgView addSubview:nextTepBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    

//    CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT)

    myBgView.frame =  CGRectMake(0, 20-120, SCREEN_WIDTH, SCREEN_HEIGHT);

    
}


- (void)tap:(UITapGestureRecognizer *)recognizer
{
    myBgView.frame =  CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT);
    [_passWord resignFirstResponder];
    [_certainPassWord resignFirstResponder];
}

-(void)nextTep
{
    if([_passWord.text length]==0){
        [self showSimpleAlertView:@"请输入密码"];
    }
    else if ([_passWord.text isEqualToString:_certainPassWord.text]==FALSE) {
        [self showSimpleAlertView:@"两次密码输入不一致"];
    }
    
    else {
        
        NSString *userId = _phoneNum;
        NSString *userCode = _passWord.text;
        [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"userId"];
        [[NSUserDefaults standardUserDefaults] setObject:userCode forKey:@"userCode"];
        NSDictionary *param = @{@"userName": _phoneNum,@"password": _passWord.text,@"authenticationCode": _checkCode};
        [UserDAL getResetPasswordParams:param
                            withBlock:^(NSDictionary *data, NSError *error) {
                                if (data) {
                                    ////NSLog(@"=======%@",data);
                                    NSString *userId = [data objectForKey:@"userId"];
                                    [[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"ID"];

                                    myTabBarViewController *myTableView = [[myTabBarViewController alloc]init];
                                    [self  presentViewController:myTableView animated:YES completion:nil];
                                }else
                                {
                                    
                                }
                            }];
    }
    //此处发送请求
    //    CreatUserInfo *cc= [[CreatUserInfo alloc]init];
    ////    [self presentViewController:cc animated:YES completion:nil];
    //    [self.navigationController pushViewController:cc animated:YES];
}


-(void)showSimpleAlertView:(NSString*)message
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示信息"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}


@end
